/*
* @Author: majingyu
* @Date:   2017-05-15 16:11:24
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-12 18:39:40
*/
import Fetch from 'common/fetch';
import { toast,confirm } from 'common/tips';
const EDIT_DATE_SHOE = 'EDIT_DATE_SHOE';
export const changeIsShow = (status) => {
    return {
        type: EDIT_DATE_SHOE,
        status
    }
}

const EDIT_DATA_VALUE = 'EDIT_DATA_VALUE';
export const changeDataShow = (date) => {
    return {
        type: EDIT_DATA_VALUE,
        date
    }
}

const EDIT_SKILL_MOBILE_SHOW = 'EDIT_SKILL_MOBILE_SHOW';
export const changeSkillMobileShow = (date) => {
    return {
        type: EDIT_SKILL_MOBILE_SHOW,
        data
    }
}
const SUBMIT_DEMAND_BEGIN = 'SUBMIT_DEMAND_BEGIN';
const SUBMIT_DEMAND_SUCCESS = 'SUBMIT_DEMAND_SUCCESS';
const SUBMIT_DEMAND_ERROR = 'SUBMIT_DEMAND_ERROR';
export const publishDemand = (params) => (dispatch, getState) => {
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch(`/wx/demand/create`,{
        method:'POST',
        headers:myHeaders,
        body:JSON.stringify(params)
    }).then(res=>res.json()).then(data=>{
        console.log(data);
        if(data.status == 200){
            toast('发布成功')
            setTimeout(()=>{
                window.location.href = '#/myPublish'
            },2000)
        }else if(data.status == 500 && data.err == '手机号不存在'){
            dispatch(changeSkillMobileShow(true));
        }else{
            toast(data.err)
            //dispatch(publish(SUBMIT_DEMAND_ERROR,data))
        }
    }).catch(e=>{
        toast('发生网络错误.')
        dispatch(publish(SUBMIT_DEMAND_ERROR,e))
    })
}

const EDIT_SERVICES_MOBILE_SHOW = 'EDIT_SERVICES_MOBILE_SHOW';
export const changeServiceMobileShow = (date) => {
    return {
        type: EDIT_SERVICES_MOBILE_SHOW,
        data
    }
}

const SUBMIT_SKILL_BEGIN = 'SUBMIT_SKILL_BEGIN';
const SUBMIT_SKILL_SUCCESS = 'SUBMIT_SKILL_SUCCESS';
const SUBMIT_SKILL_ERROR = 'SUBMIT_SKILL_ERROR';
export const publishSkill = (params) => (dispatch, getState) =>{
    //dispatch(publish(SUBMIT_SKILL_BEGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch(`/wx/skill/create`,{
        method:'POST',
        headers:myHeaders,
        body:JSON.stringify(params)
    }).then(res=>res.json()).then(data=>{
        if(data.status == 200){
            toast('发布成功')
            setTimeout(()=>{
                window.location.href = '#/publishDemand';
            },1000);
        }else if(data.status == 500 && data.err == '手机号不存在'){
            dispath(changeServiceMobileShow(true));
        }else{
            toast(data.err)
            //dispatch(publish(SUBMIT_SKILL_ERROR,data))
        }
    }).catch(e=>{
        toast('发生网络错误.')
        dispatch(publish(SUBMIT_SKILL_ERROR,e))
    })
}